package org.topic.discount;

import org.topic.user.ShoopingCart;

import java.math.BigDecimal;

/**
 * 满减折扣策略
 */
public class ThresholdDiscount implements DiscountStrategy {

    private BigDecimal threshold; // 满减门槛金额

    private BigDecimal amount;   // 减免金额

    public ThresholdDiscount(BigDecimal threshold, BigDecimal amount) {
        if (threshold.compareTo(BigDecimal.ZERO) <= 0 || amount.compareTo(BigDecimal.ZERO) <= 0) {
            throw new IllegalArgumentException("门槛金额和减免金额必须大于0");
        }
        this.threshold = threshold;
        this.amount = amount;
    }

    @Override
    public BigDecimal applyDiscount(BigDecimal subtotal, ShoopingCart cart) {
        if (subtotal.compareTo(threshold) >= 0) {
            return subtotal.subtract(amount);
        }
        return subtotal;
    }
}
